<?php

namespace App\Http\Controllers\Admin;

use App\Repositories\PostsRepositories;
use Illuminate\Http\Request;

class PagesController extends BaseController
{
    public function __construct(PostsRepositories $postsRepositories)
    {
        $this->postsRepositories = $postsRepositories;
    }

    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
       return view('admin.pages.index');
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        $pages =$this->postsRepositories->allPages();
        return view('admin.pages.create',compact('pages'));
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $data= $request->all();
        $data['post_type'] = 'page';
        $ret = $this->postsRepositories->save($data);

        if($ret)
        {
            return redirect()->route('admin.pages')->with(['success'=>'添加完成']);
        }
        return redirect()->back()->with(['success'=>'系统错误']);
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $pages =$this->postsRepositories->allPages();
        $info =$this->postsRepositories->find($id);
        return view('admin.pages.edit',compact('info','pages'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        $this->validate($request,[
            'title'  => 'required|string',

        ],[
            'title.required'=>'* 名称必传!',

        ]);

        $ret = $this->postsRepositories->update($id,$request->all());

        if($ret)
        {
            return redirect()->route('admin.pages')->with(['success'=>'更新成功！']);
        }
        return redirect()->back()->with(['success'=>'系统错误']);
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy()
    {
       $ids=\request()->input('ids');
       $ret =$this->postsRepositories->delete($ids);
       if($ret) return response()->json(['ok'=>$ret]);
        return response()->json(['ok'=>222]);
    }

    public function data(Request $request)
    {
        $data = $request->all();
        $data['post_type'] = 'page';
        $res = $this->postsRepositories->search($data);

        $listTrees = listToTree($res['data']);
        $lists = treeToOrderList($listTrees,0,'title');

        $data = [
            'code' => 0,
            'msg'   => '正在请求中...',
            'count' => $res['total'],
            'data'  => $lists
        ];

        return response()->json($data);
    }
}
